package com.greendragon.persistence.dao.impl; 

import java.util.List;

import org.apache.log4j.Logger;

import com.greendragon.persistence.dao.IResourceDao;
import com.greendragon.persistence.domain.Resource;
import com.greendragon.persistence.enmu.FetchOrderType;
import com.greendragon.persistence.enmu.ResourceType;


/** 
 * @author wulibin, exceptsky@gmail.com, 2011-9-24 下午10:25:01
 * 
 */
public class ResourceDaoImpl extends BaseDao<Resource> implements IResourceDao{
	private static Logger          logger           = Logger.getLogger(ResourceDaoImpl.class);
	
	@Override
	@SuppressWarnings("unchecked")
	public long getCountByType(ResourceType resourceType) {
		String hql = "select count(r.id) from Resource as r where r.type=?";
		try {
			List rs = getHibernateTemplate().find(hql, new Integer[]{resourceType.getIndex()});
			if(rs==null || rs.size()==0){
				return 0l;
			}
			long count = (Long)rs.listIterator().next();
			return count;
		} catch (Exception e) {
			logger.error(e);
			return 0l;
		}

	}

	@Override
	public List<Resource> getResourcesByType(ResourceType resourceType, int offset, int length, FetchOrderType orderType) {
		String hql = "from Resource as r where r.type=?"+getOrderSql("r",orderType);
		return selectByPage(hql, new Integer[]{resourceType.getIndex()}, offset, length);
	}
}
 